import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * 
 */

/**
 * Data view allows the user to view the dimensions of the current
 * brick and to change the heigh, width, or length of the field.
 * Only one value can be modified at one time.
 * 
 * @author Sophia Soohoo
 *
 */
public class DataView extends BrickView {
    
    /**Field containing length of field*/
    private JTextField lengthField = new JTextField(12);
    
    /**Field containing width of field*/
    private JTextField widthField= new JTextField(12);
    
    /**Field containing height of field*/
    private JTextField heightField= new JTextField(12);
    
    /**Opened DataView windows count */
    private static int openViewCount = 0;
    
    private DataViewController controller;
    
    /**
     * Constructor which creates the data view
     * @param model  the brick that we are observing
     */
    public DataView(BrickModel model) {
        super(model);
        this.setSize(new Dimension(100,200));
        
        this.setBorder(BorderFactory.createTitledBorder
                (BorderFactory.createLineBorder(Color.black), "Brick Attributes"));
    
        add(new JLabel("Height: "));
        add(heightField);     
        add(new JLabel("Width:  "));
        add(widthField);
        add(new JLabel("Length: "));
        add(lengthField);
        add(new JLabel("\nType text in a field and press Enter"));
        
        populateTextBoxes();


    }
    
    /**
     * Takes the values from the textboxes and populates them into the 
     * appropriate text field.
     */
    private void populateTextBoxes() {
        lengthField.setText("" + super.getModel().getLength());
        widthField.setText("" + super.getModel().getWidth());
        heightField.setText("" + super.getModel().getHeight());
    }
    
    public void setController(DataViewController controller){
        this.controller = controller;
        lengthField.addActionListener(controller);
        widthField.addActionListener(controller);
        heightField.addActionListener(controller);
    }
    
    /**
     * @return the lengthField
     */
    
    public JTextField getLengthField() {
        return lengthField;
    }

    /**
     * @return the widthField
     */
    public JTextField getWidthField() {
        return widthField;
    }

    /**
     * @return the heightField
     */
    public JTextField getHeightField() {
        return heightField;
    }
    
    public String toString(){
    	return "DataView #" + (++openViewCount);
    }

    /* (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    @Override
    public void update(Observable arg0, Object arg1) {
        populateTextBoxes();
    }
    
	/**
	 * clears the open views count attribute.
	 * Used when files are opened or a new file is created and all current views are destroyed
	 */
    public static void clearViewCount(){
    	openViewCount = 0;
    }

}
